<template>
  <el-dialog title="更新账户" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="登录帐号"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="dataForm.nickName" placeholder="昵称"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
        <span v-if="dataForm.userId" class="fontcolor">提示：不填写密码默认为不修改密码</span>
      </el-form-item>
      <el-form-item label="确认密码" prop="comfirmPassword" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.comfirmPassword" type="password" placeholder="确认密码"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :loading="btnLoading" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import seviceApi from '@/api/serviceApi.js'
import applyareaagent from '@/api/applyareaagent.js'
import { isEmail, isMobile } from '@/utils/validate'
export default {
  data () {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    var validateComfirmPassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('确认密码不能为空'))
      } else if (this.dataForm.password !== value) {
        callback(new Error('确认密码与密码输入不一致'))
      } else {
        callback()
      }
    }
    var validateEmail = (rule, value, callback) => {
      if (!isEmail(value)) {
        callback(new Error('邮箱格式错误'))
      } else {
        callback()
      }
    }
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      distribution: false,
      dataForm: {
        shopId: 0,
        userId: '',
        username: '',
        password: '',
        comfirmPassword: '',
        mobile: '',
        email: '',
        status: 1,
        nickName: ""
      },
      dataRule: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        nickName: [
          { required: true, message: '昵称不能为空', trigger: 'blur' }
        ],
        password: [
          { validator: validatePassword, trigger: 'blur' }
        ],
        comfirmPassword: [
          { validator: validateComfirmPassword, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { validator: validateEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ]
      },
      admin_id: null,
      btnLoading: false
    }
  },
  methods: {
    init (id, admin_id) {
      this.dataForm.areaAgentId = id || ""
      this.visible = true
      this.admin_id = admin_id
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (admin_id) {
        // 获取商家账号信息   getShopAdmin
        applyareaagent.getAdmin(id).then(res => {
          if (res && res.data.code == 200) {
            this.dataForm.username = res.data.data.username
            this.dataForm.mobile = res.data.data.mobile
            this.dataForm.status = res.data.data.status
            this.dataForm.email = res.data.data.email
            this.dataForm.userId = res.data.data.userId
            this.dataForm.password = res.data.data.password
            this.dataForm.comfirmPassword = res.data.data.password
            this.dataForm.nickName = res.data.data.nickName
          }
        })
      }
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let dataForm = {
            ...this.dataForm,
          };
          this.btnLoading = true;
          if (!this.admin_id) {
            applyareaagent.assignedAccount(this.dataForm).then(res => {
              if (res && res.data.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              }
              this.btnLoading = false;
            })
          } else {
            seviceApi.updateAssignedAccount(this.dataForm).then(res => {
              if (res && res.data.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              }
              this.btnLoading = false;
            })
          }
        }
      })
    }
  }
}
</script>
<style  scoped>
.fontcolor {
  font-size: 12px;
  color: #999;
}
</style>
